package edu.ujf.sharepas.rcp;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.model.WorkbenchLabelProvider;

import edu.ujf.sharepas.impl.Category;
import edu.ujf.sharepas.impl.Constante;
import edu.ujf.sharepas.impl.ShareFile;
import edu.ujf.sharepas.impl.User;

public class LibraryLabelProvider extends LabelProvider {	
	
	private Map imageCache = new HashMap(11);
	
	/*
	 * @see ILabelProvider#getImage(Object)
	 */
	@Override
	public Image getImage(Object element) {
		ImageDescriptor descriptor = null;
		if (element instanceof User) {
			descriptor = Activator.getImageDescriptor("icons/user-16x16.png");
		} else if (element instanceof Category) {
			Category cat = (Category) element ;
			
	
			if( (cat.getType())    == (Constante.CategoryType.VIDEO).toInt()){ 
				descriptor = Activator.getImageDescriptor("icons/HP-Video-Folder-Dock-16x16.png");
			}else if((cat.getType()) ==(Constante.CategoryType.AUDIO).toInt()){ 
				descriptor = Activator.getImageDescriptor("icons/HP-Music-Folder-Dock-16x16.png");
			}else if((cat.getType()) == (Constante.CategoryType.DOC).toInt()){ 
				descriptor = Activator.getImageDescriptor("icons/HP-Pictures-Folder-Dock-16x16.png");
			}else if( (cat.getType())== (Constante.CategoryType.PHOTO).toInt() ){
				descriptor = Activator.getImageDescriptor("icons/HP-Documents-Folder-Dock-16x16.png");
			}else {
				descriptor = Activator.getImageDescriptor("icons/HP-Blank-Folder-Dock-16x16.png");
			}

		} else if (element instanceof IFile) {
			//FIXME descriptor = Activator.getImageDescriptor("gameboard.gif");
			return null;
		} else {
			throw unknownElement(element);
		}

		//obtain the cached image corresponding to the descriptor
		Image image = (Image)imageCache.get(descriptor);
		if (image == null) {
			image = descriptor.createImage();
			imageCache.put(descriptor, image);
		}
		return image;
	}

	/*
	 * @see ILabelProvider#getText(Object)
	 */
	public String getText(Object element) {
		if (element instanceof User) {
			if(((User)element).getName() == null) {
				return "Unknown User";
			} else {
				return ((User)element).getName();
			}
		} else if (element instanceof Category) {
			return ((Category)element).getName();
		} else if (element instanceof IFile) {
			return ((IFile) element).getName();
		} else {
			throw unknownElement(element);
		}
	}

	public void dispose() {
		for (Iterator i = imageCache.values().iterator(); i.hasNext();) {
			((Image) i.next()).dispose();
		}
		imageCache.clear();
	}

	protected RuntimeException unknownElement(Object element) {
		return new RuntimeException("Unknown type of element in tree of type " + element.getClass().getName());
	}
	

}
